package com.mayikt.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.constant.WeiXinConstant;
import com.mayikt.mp.config.WxMpConfiguration;
import com.mayikt.mp.config.WxMpProperties;
import com.mayikt.service.WeiXinQrCodeService;
import me.chanjar.weixin.mp.api.WxMpQrcodeService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;

/**
 * @Name WeiXinQrCodeServiceImpl
 * @Author 枫火
 * @Date 2021/5/21 18:31
 * @Description
 **/
@RestController
public class WeiXinQrCodeServiceImpl extends BaseApiService implements WeiXinQrCodeService {

    @Autowired
    private WxMpProperties wxMpProperties;

    @Override
    public BaseResponse<JSONObject> getQrUrl(Long userId) {
        if (userId == null) {
            return setResultError("userId不能为空");
        }
        // 获取配置第一个appId
        String appId = wxMpProperties.getConfigs().get(0).getAppId();
        // 根据appId获取对应的WxMpQrcodeService
        WxMpQrcodeService qrcodeService = WxMpConfiguration.getMpServices().get(appId).getQrcodeService();
        try {
            WxMpQrCodeTicket wxMpQrCodeTicket = qrcodeService.qrCodeCreateTmpTicket(userId + "",
                    WeiXinConstant.QR_CODE_EXPIRE_SECONDS);
            if (wxMpQrCodeTicket == null) {
                return setResultError("生成二维码链接失败!");
            }
            String ticket = wxMpQrCodeTicket.getTicket();
            return setResultSuccess(URLEncoder.encode(ticket, "UTF-8"));
        } catch (Exception e) {
            return setResultError("生成二维码链接失败!");
        }
    }
}
